package com.zmx.doctor.view.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.wang.adapters.interfaceabstract.OnItemClickListener;
import com.zmx.doctor.BaseActivity;
import com.zmx.doctor.R;
import com.zmx.doctor.adapter.DiseaseNameAdapter;
import com.zmx.doctor.adapter.DotorAdapter;
import com.zmx.doctor.okhttp.OkHttp3ClientManager;
import com.zmx.doctor.pojo.DiseaseName;
import com.zmx.doctor.pojo.SearchDoctor;
import com.zmx.doctor.utils.AllUtil;
import com.zmx.doctor.view.util.MySharedPreferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 程序猿：胖胖祥
 * 时 间：2020/10/17  1:22
 * 功 能：疾病搜索
 */
public class DiseaseNameActivity extends BaseActivity {


    @BindView(R.id.registered)
    TextView registered;
    @BindView(R.id.title)
    EditText title;
    @BindView(R.id.reView)
    RecyclerView reView;


    private DiseaseNameAdapter adapter;
    private List<DiseaseName> lists;



    @Override
    protected int getLayoutId() {
        return R.layout.activity_disease_name;
    }

    @Override
    protected void initViews() {

        ButterKnife.bind(this);
        setToolbar(R.id.tool_bar);

        lists = new ArrayList<>();
        adapter = new DiseaseNameAdapter(mActivity);
        reView.setLayoutManager(new LinearLayoutManager(this));
        reView.setAdapter(adapter);

        searchUsre("");

        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            protected void onItemClick(View view, int listPosition) {
                title.setText(lists.get(listPosition).getName());
            }
        });

        title.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {

                searchUsre(title.getText().toString());

            }

        });

    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what){

                case 1:

                    lists.clear();

                    try {

                        JSONObject object = new JSONObject(msg.obj.toString());
                        Log.e("用户id","用户id"+object.toString());
                        Gson gson = new Gson();
                        JSONArray array = object.getJSONArray("data");
                        for (int i = 0;i<array.length();i++){

                            DiseaseName doctor = gson.fromJson(array.getJSONObject(i).toString(),DiseaseName.class);
                            lists.add(doctor);

                        }

                        adapter.setListAndNotifyDataSetChanged(lists);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;

            }

        }
    };

    @OnClick(R.id.registered)
    public void onViewClicked() {

        Intent intent = new Intent();
        intent.putExtra("name", title.getText().toString()); //放置要传出的数据
        //这里是在Recycleview的适配器里，传了一个Activity才能用方法setResult
        this.setResult(1,intent);
        this.finish(); //关闭活动

    }

    public void searchUsre(String disease_name) {

        List<String> ls = new ArrayList<>();
        ls.add(disease_name);
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(ls)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "searchDisease");
        params.put("data[disease_name]", disease_name);
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/doctor/", params, handler, 1, 404);

    }
}
